package com.yjhb.android.sbz.activity;

import android.widget.EditText;
import android.widget.TextView;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.mvp.settinginfo.BindPhonePresenter;
import com.yjhb.android.sbz.mvp.settinginfo.BindPhoneView;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.ZxDataTool;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 创建者 zx
 * 创建时间 2020/11/24 12:05
 */
public class BindPhoneActivity extends BaseActivity<BindPhonePresenter> implements BindPhoneView {
    private final String TAG = "BindPhoneActivity";

    @BindView(R.id.et_tel)
    EditText et_tel;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.getcode_tv)
    TextView getcode_tv;

    private Disposable disposable;

    @Override
    protected BindPhonePresenter initPresenter() {
        return new BindPhonePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bindphone;
    }

    @Override
    protected void initUI() {
        setStatusBarLightMode(true);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void regEvent() {

    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }


    @OnClick(R.id.confirm_tv)
    public void confirm_tv() {
        if (Tool.isEditEmpty(et_tel)) {
            ZxToast.showBottomToastLight("请输入手机号码");
            return;
        }
        if (Tool.isEditEmpty(et_code)) {
            ZxToast.showBottomToastLight("请输入验证码");
            return;
        }
        presenter.VerificationPhone(et_tel.getText().toString(), et_code.getText().toString());
    }

    @OnClick(R.id.getcode_tv)
    public void getcode_tv() {
        if (Tool.isEditEmpty(et_tel)) {
            ZxToast.showBottomToastLight("请输入手机号码");
            return;
        }
        if (!ZxDataTool.isMobile(et_tel.getText().toString())) {
            ZxToast.showBottomToastLight("请输入正确的手机号");
            return;
        }
        presenter.sendCode(et_tel.getText().toString());
    }


    @Override
    public void onBindSuccess() {
        loadingDialog.cancel();
        ZxToast.showBottomToastLight("绑定成功");
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE, null));
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void onWait() {
        loadingDialog.show();
    }

    @Override
    public void onCodeSendSuccess() {
        loadingDialog.cancel();
        offGetCode();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        ZxToast.showINFOBottomToast(msg);
    }

    @Override
    protected void onDestroy() {
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
        disposable = null;
        super.onDestroy();
    }

    /**
     * 关闭获取验证码
     */
    private void offGetCode() {
        getcode_tv.setEnabled(false);
        Observable.intervalRange(0, 60, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        getcode_tv.setText("" + (60 - aLong) + "s后重新获取");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        getcode_tv.setEnabled(true);
                        getcode_tv.setText("获取验证码");
                    }
                });
    }

}
